import {Component, OnInit, ViewChild, TemplateRef} from '@angular/core';
import {BtnsGen, RequestInfo, SelectorGen, StatusSelector, TableGen} from '@list-page';
import {NzMessageService} from 'ng-zorro-antd';
import {RoleManagmentService} from './role-managment.service';
import {Router} from '@angular/router';

@Component({
    templateUrl: './role-managment.component.html',
    styleUrls: ['../user-managment/user-managment.component.less']
})
export class RoleManagmentComponent implements OnInit {
    selectorGen: SelectorGen;
    tableGen: TableGen;
    statusSelector: StatusSelector;
    btnsGen: BtnsGen;
    queryReqInfo: RequestInfo;
    funSet: any = {};
    isVisibleDetails = false;
    selectedRole: any;
    @ViewChild('operationTpl') operationTpl: TemplateRef<void>;

    constructor(private roleManaservice: RoleManagmentService,
                private message: NzMessageService,
                private router: Router) {
    }

    ngOnInit() {
        // 筛选条件
        this.selectorGen = {
            selectorArr: [
                {
                    label: '角色名',
                    key: 'name',
                    placeholder: '请输入角色名'
                },
            ],
            splitScale: 3,
            isCollapse: false,
            btnPosition: 'topRight',
            dataProcess: (selectedMap) => {
                return selectedMap;
            }
        };
        // 按钮管理
        this.btnsGen = {
            right: [
                {
                    name: '添加',
                    zorroType: 'default',
                    klClick: this.addRole
                }
            ]
        };
        // 列表管理
        this.tableGen = {
            cols: [
                {head: 'ID', prop: 'id'},
                {head: '角色名', prop: 'name'},
                {head: 'CODE', prop: 'code'},
                {head: '创建时间', prop: 'createTime', type: 'date', format: 'yyyy/MM/dd', sort: ''},
                {head: '操作', prop: 'operation', type: 'template'}
            ],
            serialNumber: true,
            noChecked: true,
            dataProcess: (data, funset) => {
                data.map(item => {
                    item.operation = this.operationTpl;
                });
                return data;
            }
        };
        // 列表请求设置
        this.queryReqInfo = {
            path: '/roles',
            params: {}, // 静态参数，不会改变的
            method: 'GET'
        };
    }

    // 添加角色
    addRole = (data, funSet) => {
        this.isVisibleDetails = true;
        this.setSelectedRole(void 0);
        this.funSet = funSet;
    };

    // 修改用户信息
    modifyUser(data, funSet) {
        this.setSelectedRole(data);
        this.isVisibleDetails = true;
        this.funSet = funSet;
    };

    setSelectedRole(roleInfo) {
        this.selectedRole = roleInfo;
    }

    handleProcessRst(rst) {
        if (rst) {
            this.funSet.refreshList();
        }
    }

    deleteUser(data, funSet) {
        this.roleManaservice.deleteUser(data.id).then(rep => {
            if (rep.resp_code === '10000') {
                this.message.success(`删除用户 ${data.name} 成功`);
                funSet.refreshList();
            } else {
                this.message.error(`删除用户 ${data.name} 失败，${rep.resp_msg}`);
            }
        })
    }
    // 权限分配
    resetPassword(data) {
        this.router.navigate(['home/identity-managment/role-managment/authorization-list'], {queryParams: {id: data.id}})
    }
}
